package com.cdwm.mrp.common.config.mvc;

import com.cdwm.mrp.common.tools.UploadPathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;

/**
 * @version v1.0
 * @Author: lyw
 * @Date: 2021/8/3
 */
@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {

    // 文档上传路径
    @Value("${file.staticAccessDocumentPath}")
    private String staticAccessDocumentPath;

    // 图片上传路径
    @Value("${file.staticAccessImagesPath}")
    private String staticAccessImagesPath;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 登录请求访问登录页面
        registry.addViewController("/toLogin").setViewName("login.html");
    }

    /**
     * 虚拟路径映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        ArrayList<String> systemRoot = UploadPathUtil.getSystemRoot();

        log.info("systemRoot: "+systemRoot);
        // 在yml配置文件中读取路径
        registry.addResourceHandler(staticAccessDocumentPath).addResourceLocations("file:"+systemRoot.get(0));

        registry.addResourceHandler(staticAccessImagesPath).addResourceLocations("file:"+systemRoot.get(1));
    }
}

